package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.StuUserMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.IStuUserService;
import com.bishe.psy.utils.MD5Util;
import com.bishe.psy.utils.StringUtil;
import com.bishe.psy.utils.UUIDUtil;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-03-24
 * Time: 17:36
 */

@Slf4j
@Service
public class StuUserServiceImpl implements IStuUserService {
    @Resource
    private StuUserMapper stuUserMapper;

    //新增用户
    @Override
    public void createStuUser(StuUser stuuser) {

        //1.参数校验
        if(stuuser == null || StringUtil.isEmpty(stuuser.getUsername())
        || StringUtil.isEmpty(stuuser.getPassword()) || StringUtil.isEmpty(stuuser.getNickname())
        || StringUtil.isEmpty(stuuser.getGender()) || StringUtil.isEmpty(stuuser.getSalt())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据用户名查询用户是否存在
        StuUser stuUsername = stuUserMapper.StuSelectByUsername(stuuser.getUsername());

        if(stuUsername != null) {
            log.warn(ResultCode.AILED_USER_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.AILED_USER_EXISTS));
        }

        //3.往数据库插入新的用户信息
        //设置默认值
        stuuser.setDeleteState((byte) 0);
        Date data = new Date();
        stuuser.setCreateState(data);
        stuuser.setUpdateState(data);
        //动态插入信息
        int row = stuUserMapper.insertSelective(stuuser);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        log.info("新增用户成功");
    }


    //根据用户名查询用户
    @Override
    public StuUser selectByUsername(String username) {
        if(StringUtil.isEmpty(username)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return stuUserMapper.StuSelectByUsername(username);
    }

    @Override
    public StuUser login(String username, String password) {
        //1.查询用户名是否存在
        StuUser stuUser = selectByUsername(username);
        System.out.println(stuUser);

        if(stuUser == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //2.密码校验
        String salt = stuUser.getSalt();
        String ciphertext = MD5Util.md5(password,salt);

        //输入密码不正确
        if(!ciphertext.equals(stuUser.getPassword())) {
            log.warn(ResultCode.FAILED_LOGIN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }

        return stuUser;
    }

    @Override
    public StuUser selectByPrimaryKey(Long id) {
        //校验参数
        if(id == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        StuUser stuUser = stuUserMapper.selectByPrimaryKey(id);

        return stuUser;
    }

    @Override
    public void modifyStuInfo(StuUser stuUser) {

        System.out.println(stuUser);

        //1.参数校验
        if(stuUser == null || stuUser.getId() == null || stuUser.getId() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据当前id查询用户是是否存在
        StuUser selectStuUser1 = stuUserMapper.selectByPrimaryKey(stuUser.getId());

        //3.判断当前用户是否存在
        if(selectStuUser1 == null || selectStuUser1.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //4.创建一个更新对象
        StuUser updateStuUser = new StuUser();
        updateStuUser.setId(selectStuUser1.getId());

        //5.判断是否能够调用更新语句的字段
        boolean flag = false;

        //6.当昵称、邮箱地址、电话号码、年级、学院、专业,个人简介，存在一个字段被修改，则可以调用数据库修改语句
        //当上述字段都没有被修改时，则不能掉用数据库语句

        if(!StringUtil.isEmpty(stuUser.getNickname()) && !stuUser.getNickname().equals(selectStuUser1.getNickname())) {
           updateStuUser.setNickname(stuUser.getNickname());
           flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getEmail()) && !stuUser.getEmail().equals(selectStuUser1.getEmail())) {
            updateStuUser.setEmail(stuUser.getEmail());
            flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getPhoneNum()) && !stuUser.getPhoneNum().equals(selectStuUser1.getPhoneNum())) {
            updateStuUser.setPhoneNum(stuUser.getPhoneNum());
            flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getGrade()) && !stuUser.getGrade().equals(selectStuUser1.getGrade())) {
            updateStuUser.setGrade(stuUser.getGrade());
            flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getAcademy()) && !stuUser.getAcademy().equals(selectStuUser1.getAcademy())) {
            updateStuUser.setAcademy(stuUser.getAcademy());
            flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getSpecialized()) && !stuUser.getSpecialized().equals(selectStuUser1.getSpecialized())) {
            updateStuUser.setSpecialized(stuUser.getSpecialized());
            flag = true;
        }
        if(!StringUtil.isEmpty(stuUser.getRemark()) && !stuUser.getRemark().equals(selectStuUser1.getRemark())) {
            updateStuUser.setRemark(stuUser.getRemark());
            flag = true;
        }

        //7.判断是否可以调用数据库
        if(flag == false) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //8.调用数据库
        int row = stuUserMapper.updateByPrimaryKeySelective(updateStuUser);

        //9.判断是否更新成功
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public void modifyPassword(Long id, String oldPassword, String newPassword) {

        //1.参数校验
        if(id == null || id <= 0 || StringUtil.isEmpty(oldPassword) || StringUtil.isEmpty(newPassword)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据id查询用户信息
        StuUser stuUser = stuUserMapper.selectByPrimaryKey(id);

        //3.判断用户是否存在
        if(stuUser == null || stuUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //4.判断输入的旧密码是否与表中保存的一致
        String oldEncryptPassword = MD5Util.md5(oldPassword,stuUser.getSalt());
        if(!oldEncryptPassword.equalsIgnoreCase(stuUser.getPassword())) {
            log.warn(ResultCode.FAILED_OLD_PASSWORD.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_OLD_PASSWORD));
        }

        //5.对新密码进行MD5加密
        String newSalt = UUIDUtil.UUID_32();
        String encryptPassword = MD5Util.md5(newPassword,newSalt);

        //6.更新修改内容
        StuUser updateStu = new StuUser();
        updateStu.setId(stuUser.getId());
        updateStu.setSalt(newSalt);
        updateStu.setPassword(encryptPassword);
        Date date = new Date();
        updateStu.setUpdateState(date);

        //7.调用数据库
        int row = stuUserMapper.updateByPrimaryKeySelective(updateStu);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public List<StuUser> selectAllInfo() {

        //1.调用数据库
        List<StuUser> stuUsers = stuUserMapper.selectAllInfo();

        //2.返回结果
        return stuUsers;

    }

    @Override
    public void updateDeleteStateByUsername(String username) {

        //1.非空校验
        if(StringUtil.isEmpty(username)) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //2.根据用户名查询用户
        StuUser stuUser = stuUserMapper.StuSelectByUsername(username);
        if(stuUser == null || stuUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //3.封装修改对象
        StuUser updateStu = new StuUser();
        updateStu.setId(stuUser.getId());
        updateStu.setDeleteState((byte) 1);
        updateStu.setUpdateState(new Date());

        //4.调用数据库
        int row = stuUserMapper.updateByPrimaryKeySelective(updateStu);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }


    }

}
